package com.wenkang.stu.util;

import com.wenkang.stu.enums.ExceptionEnums;
import org.apache.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class ResponseUtils {

    public static void ExceptionResult(HttpServletResponse response, String msg) throws IOException {
        response.setStatus(HttpStatus.SC_BAD_REQUEST);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        Map<String, Object> map = new HashMap<>();
        map.put("msg", msg);
        map.put("timestamp", System.currentTimeMillis());
        map.put("status", 400);
        writer.write(JsonUtils.toString(map));
        writer.flush();
        writer.close();
    }

    public static void ExceptionResult(HttpServletResponse response, ExceptionEnums enums) throws IOException {
        response.setStatus(enums.getCode());
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        Map<String, Object> map = new HashMap<>();
        map.put("msg", enums.getMsg());
        map.put("timestamp", System.currentTimeMillis());
        map.put("status", enums.getCode());
        writer.write(JsonUtils.toString(map));
        writer.flush();
        writer.close();
    }
}
